/*
 * Copyright (c) 2025 彭友聪
 * nativePC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
            http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 *
 * Author: 彭友聪
 * email：2923616405@qq.com
 * date: 2025/11/30 09:56
 * file: ${FILE_NAME}
 * product: DevEco Studio
 * LICENSE: MulanPSL-2.0
 * */
//
// Created on 2025/11/30.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#ifndef NATIVEPC_TESTANIMATE_H
#define NATIVEPC_TESTANIMATE_H
#include "ArkUIBaseNode.h"
#include "ArkUIButtonNode.h"
#include "ArkUIColumnNode.h"
#include "ArkUITextInputNode.h"
#include "ArkUITextNode.h"
#include "NativeEntry.h"
namespace NativeModule
{
    std::shared_ptr<ArkUIButtonNode> g_animator_button = nullptr;
    std::shared_ptr<ArkUITextNode> g_animator_text = nullptr;
    ArkUI_AnimatorHandle animatorHandle = nullptr;
    std::shared_ptr<ArkUIBaseNode> testAnimate()
    {
        auto column = std::make_shared<ArkUIColumnNode>();
        column->SetPercentHeight(1);
        column->SetPercentWidth(1);
        column -> SetItemAlign(ARKUI_HORIZONTAL_ALIGNMENT_CENTER);
        column -> SetJustifyContent(ARKUI_FLEX_ALIGNMENT_START);
        // 创建文本节点，内容区介绍“这是Animator动画”
        auto textNode = std::make_shared<ArkUITextNode>();
        textNode->SetTextContent("这是Animator动画");
        textNode->SetWidth(150);
        textNode->SetHeight(50);
        // 创建createButton，用于初始化Animator参数
        auto createButton = std::make_shared<ArkUIButtonNode>();
        // 创建button，后续创建的Animator动画作用在button组件上
        auto button = std::make_shared<ArkUIButtonNode>();
        // 设置button初始宽高
        button->SetWidth(100);
        button->SetHeight(100);
        // 存储button全局变量，在onTouch注册时需要使用
        g_animator_button = button;
        // 注册点击事件到button上
        createButton->RegisterNodeEvent(createButton->GetHandle(), NODE_ON_CLICK, 3, nullptr);
        g_animator_text = std::make_shared<ArkUITextNode>();
        g_animator_text->AnimatorToString();
        auto onTouch = [](ArkUI_NodeEvent *event)
        {
            // 点击button按钮时触发该逻辑
            if (OH_ArkUI_NodeEvent_GetTargetId(event) == 3) {
                // 获取context对象
                static ArkUI_ContextHandle context = nullptr;
                context = OH_ArkUI_GetContextByNode(g_animator_button->GetHandle());

                // 获取ArkUI_NativeAnimateAPI接口
                auto animateApi = NativeModuleInstance::GetInstance()->GetNativeAnimateAPI();

                // 以下代码为创建Animator动画的关键流程，包括设置Animator动画参数、开启Animator动画
                // 设置ArkUI_AnimatorOption参数，通过提供的C方法设置对应的参数
                static ArkUI_AnimatorOption *option = OH_ArkUI_AnimatorOption_Create(0); // Animator动画状态数
                OH_ArkUI_AnimatorOption_SetDuration(option, 2000);
                OH_ArkUI_AnimatorOption_SetDelay(option, 10);
                OH_ArkUI_AnimatorOption_SetIterations(option, 3);
                OH_ArkUI_AnimatorOption_SetFill(option, ARKUI_ANIMATION_FILL_MODE_NONE);
                OH_ArkUI_AnimatorOption_SetDirection(option, ARKUI_ANIMATION_DIRECTION_NORMAL);
                ArkUI_CurveHandle curve =
                    OH_ArkUI_Curve_CreateCubicBezierCurve(0.5f, 4.0f, 1.2f, 0.0f); // 构造三阶贝塞尔曲线对象
                OH_ArkUI_AnimatorOption_SetCurve(option, curve);
                OH_ArkUI_AnimatorOption_SetBegin(option, 100);
                OH_ArkUI_AnimatorOption_SetEnd(option, 150);
                ArkUI_ExpectedFrameRateRange *range = new ArkUI_ExpectedFrameRateRange;
                range->max = 120;
                range->expected = 60;
                range->min = 30;
                OH_ArkUI_AnimatorOption_SetExpectedFrameRateRange(option, range);
                OH_ArkUI_AnimatorOption_SetKeyframe(option, 0.5, 120.5, 0); // 设置Animator动画关键帧参数
                OH_ArkUI_AnimatorOption_SetKeyframeCurve(option, curve, 0); // 设置Animator动画关键帧曲线类型
                OH_ArkUI_AnimatorOption_RegisterOnFrameCallback(
                    option, nullptr,
                    [](ArkUI_AnimatorOnFrameEvent *event)
                    {
                        OH_ArkUI_AnimatorOnFrameEvent_GetUserData(event); // 获取动画事件对象中的用户自定义对象
                        auto value = OH_ArkUI_AnimatorOnFrameEvent_GetValue(event); // 获取动画事件对象中的当前进度
                        OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Init",
                                     "CXX OH_ArkUI_AnimatorOption_RegisterOnFrameCallback  %{public}f", value);
                        g_animator_button->SetWidth(value);
                    });
                OH_ArkUI_AnimatorOption_RegisterOnFinishCallback(option, reinterpret_cast<void *>(option),
                                                                 [](ArkUI_AnimatorEvent *event)
                                                                 {
                                                                     OH_ArkUI_AnimatorEvent_GetUserData(
                                                                         event); // 获取动画事件对象中的用户自定义对象
                                                                 });
                OH_ArkUI_AnimatorOption_RegisterOnCancelCallback(option, nullptr, [](ArkUI_AnimatorEvent *event) {});
                OH_ArkUI_AnimatorOption_RegisterOnRepeatCallback(option, nullptr, [](ArkUI_AnimatorEvent *event) {});
                // 执行对应的动画
                animatorHandle = animateApi->createAnimator(context, option);

                auto duration = OH_ArkUI_AnimatorOption_GetDuration(option);
                auto delay = OH_ArkUI_AnimatorOption_GetDelay(option);
                auto iterations = OH_ArkUI_AnimatorOption_GetIterations(option);
                auto fill = OH_ArkUI_AnimatorOption_GetFill(option);
                auto direction = OH_ArkUI_AnimatorOption_GetDirection(option);
                auto curves = OH_ArkUI_AnimatorOption_GetCurve(option); // 获取Animator动画插值曲线
                auto begin = OH_ArkUI_AnimatorOption_GetBegin(option);
                auto end = OH_ArkUI_AnimatorOption_GetEnd(option); // 获取Animator动画插值终点
                auto expected =
                    OH_ArkUI_AnimatorOption_GetExpectedFrameRateRange(option); // 获取关键帧动画参数的期望帧率
                auto keyframeTime = OH_ArkUI_AnimatorOption_GetKeyframeTime(option, 0); // 获取Animator动画关键帧时间
                auto keyframeValue = OH_ArkUI_AnimatorOption_GetKeyframeValue(option, 0); // 获取Animator动画关键帧数值
                auto keyframeCurve =
                    OH_ArkUI_AnimatorOption_GetKeyframeCurve(option, 0); // 获取Animator动画关键帧动画插值曲线
                g_animator_text->AnimatorToString(duration, delay, iterations, fill, direction, begin, end, *expected,
                                                  keyframeTime, keyframeValue);
            }
        };

        // 注册点击事件的回调函数
        createButton->RegisterNodeEventReceiver(onTouch);
        createButton->SetLabel("create");
        // 创建容器，用于存放button按键
        auto buttonColumn = std::make_shared<ArkUIColumnNode>();
        buttonColumn->SetPadding(30, false); // 设置布局格式，调整组件内间距
        buttonColumn->SetWidth(300);
        // 创建容器，用于存放playButton按键
        auto playButtonColumn = std::make_shared<ArkUIColumnNode>();
        playButtonColumn->SetPadding(10, false); // 设置布局格式，调整组件内间距
        playButtonColumn->SetWidth(300);
        // 设置Animator播放按钮
        auto playButton = std::make_shared<ArkUIButtonNode>();
        playButton->SetLabel("play");
        playButton->RegisterNodeEvent(playButton->GetHandle(), NODE_ON_CLICK, 4, nullptr);
        auto onTouchPlay = [](ArkUI_NodeEvent *event)
        {
            // 点击button按钮时触发该逻辑
            if (OH_ArkUI_NodeEvent_GetTargetId(event) == 4) {
                OH_ArkUI_Animator_Play(animatorHandle);
            }
        };
        playButton->RegisterNodeEventReceiver(onTouchPlay);
        // 设置Animator结束按钮
        auto finishButton = std::make_shared<ArkUIButtonNode>();
        finishButton->SetLabel("finish");
        finishButton->RegisterNodeEvent(finishButton->GetHandle(), NODE_ON_CLICK, 5, nullptr);
        auto onTouchFinish = [](ArkUI_NodeEvent *event)
        {
            // 点击button按钮时触发该逻辑
            if (OH_ArkUI_NodeEvent_GetTargetId(event) == 5) {
                OH_ArkUI_Animator_Finish(animatorHandle);
            }
        };
        finishButton->RegisterNodeEventReceiver(onTouchFinish);
        // 创建容器，用于存放resetButton按键
        auto resetButtonColumn = std::make_shared<ArkUIColumnNode>();
        resetButtonColumn->SetPadding(10, false); // 设置布局格式，调整组件内间距
        resetButtonColumn->SetWidth(300);
        // 设置Animator更新按钮
        auto resetButton = std::make_shared<ArkUIButtonNode>();
        resetButton->SetLabel("reset");
        resetButton->RegisterNodeEvent(resetButton->GetHandle(), NODE_ON_CLICK, 6, nullptr);
        auto onTouchReset = [](ArkUI_NodeEvent *event)
        {
            // 点击button按钮时触发该逻辑
            if (OH_ArkUI_NodeEvent_GetTargetId(event) == 6) {
                static ArkUI_AnimatorOption *option = OH_ArkUI_AnimatorOption_Create(0); // Animator动画状态数
                OH_ArkUI_AnimatorOption_SetDuration(option, 1000);
                OH_ArkUI_AnimatorOption_SetDelay(option, 0);
                OH_ArkUI_AnimatorOption_SetIterations(option, 4);
                // 根据自己得需要选择下述两种曲线适合得去设置OH_ArkUI_AnimatorOption_SetCurve
                auto curve =
                    OH_ArkUI_Curve_CreateCurveByType(ARKUI_CURVE_EASE); // 动画以低速开始，然后加快，在结束前变慢
                auto stepsCurve = OH_ArkUI_Curve_CreateStepsCurve(20, true); // 构造阶梯曲线对象
                OH_ArkUI_AnimatorOption_SetCurve(option, curve);
                OH_ArkUI_AnimatorOption_SetBegin(option, 200);
                OH_ArkUI_AnimatorOption_SetEnd(option, 100);
                OH_ArkUI_AnimatorOption_RegisterOnFrameCallback(
                    option, nullptr,
                    [](ArkUI_AnimatorOnFrameEvent *event)
                    {
                        OH_ArkUI_AnimatorOnFrameEvent_GetUserData(event); // 获取动画事件对象中的用户自定义对象
                        auto value = OH_ArkUI_AnimatorOnFrameEvent_GetValue(event); // 获取动画事件对象中的当前进度
                        g_animator_button->SetWidth(value);
                    });
                OH_ArkUI_Animator_ResetAnimatorOption(animatorHandle, option);
            }
        };
        resetButton->RegisterNodeEventReceiver(onTouchReset);
        // 设置Animator暂停按钮
        auto pauseButton = std::make_shared<ArkUIButtonNode>();
        pauseButton->SetLabel("pause");
        pauseButton->RegisterNodeEvent(pauseButton->GetHandle(), NODE_ON_CLICK, 7, nullptr);
        auto onTouchPause = [](ArkUI_NodeEvent *event)
        {
            // 点击button按钮时触发该逻辑
            if (OH_ArkUI_NodeEvent_GetTargetId(event) == 7) {
                OH_ArkUI_Animator_Pause(animatorHandle);
            }
        };
        pauseButton->RegisterNodeEventReceiver(onTouchPause);
        // 创建容器，用于存放cancelButton按键
        auto cancelButtonColumn = std::make_shared<ArkUIColumnNode>();
        cancelButtonColumn->SetPadding(10, false); // 设置布局格式，调整组件内间距
        cancelButtonColumn->SetWidth(300);
        // 设置Animator取消按钮
        auto cancelButton = std::make_shared<ArkUIButtonNode>();
        cancelButton->SetLabel("cancel");
        cancelButton->RegisterNodeEvent(cancelButton->GetHandle(), NODE_ON_CLICK, 8, nullptr);
        auto onTouchCancel = [](ArkUI_NodeEvent *event)
        {
            // 点击button按钮时触发该逻辑
            if (OH_ArkUI_NodeEvent_GetTargetId(event) == 8) {
                OH_ArkUI_Animator_Cancel(animatorHandle);
            }
        };
        cancelButton->RegisterNodeEventReceiver(onTouchCancel);
        // 设置Animator以相反的顺序播放按钮
        auto reverseButton = std::make_shared<ArkUIButtonNode>();
        reverseButton->SetLabel("reverse");
        reverseButton->RegisterNodeEvent(reverseButton->GetHandle(), NODE_ON_CLICK, 9, nullptr);
        auto onTouchReverse = [](ArkUI_NodeEvent *event)
        {
            // 点击button按钮时触发该逻辑
            if (OH_ArkUI_NodeEvent_GetTargetId(event) == 9) {
                OH_ArkUI_Animator_Reverse(animatorHandle);
            }
        };
        reverseButton->RegisterNodeEventReceiver(onTouchReverse);
        // 将button挂载在column上，返回column节点
        column->AddChild(g_animator_text);
        column->AddChild(textNode);
        column->AddChild(button);
        buttonColumn->AddChild(createButton);
        playButtonColumn->AddChild(playButton);
        buttonColumn->AddChild(playButtonColumn);
        buttonColumn->AddChild(finishButton);
        resetButtonColumn->AddChild(resetButton);
        buttonColumn->AddChild(resetButtonColumn);
        buttonColumn->AddChild(pauseButton);
        cancelButtonColumn->AddChild(cancelButton);
        buttonColumn->AddChild(cancelButtonColumn);
        buttonColumn->AddChild(reverseButton);
        column->AddChild(buttonColumn);
        return column;
    }
} // namespace NativeModule

#endif // NATIVEPC_TESTANIMATE_H
